package cz.muni.fi.pa165.creatures.model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * This class represents a creature which lives in a {@link Region}.
 *
 * @author xmiklos1
 */
@Entity
@Table(name = "CREATURE")
public class Creature implements Serializable {

    /**
     * Id of the particular creature.
     */
    private Long id;
    
    /**
     * The name of a creature. At least it's name has to be set.
     */
    private String name;
    
    /**
     * The height of a creature in centimeters.
     */
    private Integer height;
    
    /**
     * The weight of a creature in centimeters.
     */
    private Integer weight;
    
    /**
     * The region where a creature lives.
     */
    private Region region;
    
    /**
     * The type of the creature.
     */
    private CreatureType creatureType;

    /**
     * Actual hit points of a creatures which
     * reflects its injuries and health condition.
     * 
     * Value of {@code actualHitPoints} is initially set to 
     * {@link CreatureType#maxHitPoints}
     */
    private Integer actualHitPoints;
    
    /**
     * Default constructor
     */
    public Creature() {
    }

    /**
     *
     * @param name name of a creature
     */
    public Creature(String name) {
        this.name = name;
    }

    /**
     * Gets id of a creature.
     * 
     * @return id of a creature.
     */
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", nullable = false)
    public Long getId() {
        return id;
    }

    /**
     * Sets id of a creature.
     *
     * @param id id of a creature
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * Gets name of a creature.
     *
     * @return name of a creature
     */
    @Column(name = "NAME", nullable = false, length=255)
    public String getName() {
        return name;
    }

    /**
     * Sets name of a creature. Name of a creature can't be null.
     *
     * @param name name of a creature
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets height of a creature in centimeters.
     *
     * @return height of a creature in centimeters.
     */
    @Column(name = "HEIGHT")
    public Integer getHeight() {
        return height;
    }

    /**
     * Sets height of a creature in centimeters.
     *
     * @param height height of a creature in centimeters
     */
    public void setHeight(Integer height) {
        this.height = height;
    }

    /**
     * Gets weight of a creature in kilograms.
     *
     * @return weight of a creature in kilograms
     */
    @Column(name = "WEIGHT")
    public Integer getWeight() {
        return weight;
    }

    /**
     * Sets weight of a creature in kilograms.
     *
     * @param weight weight of a creature in kilograms
     */
    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    /**
     * Gets the region where a creature lives.
     *
     * @return region where a creature lives
     */
    @ManyToOne
    @JoinColumn(name = "REGION_ID")
    public Region getRegion() {
        return region;
    }

    /**
     * Sets the region where a creature lives.
     *
     * @param region region where a creature lives
     */
    public void setRegion(Region region) {
        this.region = region;
    }

    /**
     * Gets the type of a creature.
     *
     * @return type of a creature
     */
    @ManyToOne
    @JoinColumn(name = "CREATURE_TYPE_ID")
    public CreatureType getCreatureType() {
        return creatureType;
    }

    /**
     * Sets the type of a creature.
     *
     * @param type type of a creature
     */
    public void setCreatureType(CreatureType creatureType) {
        this.creatureType = creatureType;
    }

    /**
     * Gets actual hit points of a creature
     * 
     * @return actual health of a creature
     */
    public Integer getActualHitPoints() {
        return actualHitPoints;
    }

    /**
     * Sets actual hit points of a creature to {@code actualHiPoints}.
     * 
     * @param actualHitPoints 
     */
    public void setActualHitPoints(Integer actualHitPoints) {
        this.actualHitPoints = actualHitPoints;
    }

    @Override
    public String toString() {
        return "Creature{" + "id=" + id + ", name=" + name + ", height="
                + height + ", weight=" + weight + ", region=" + region
                + ", creatureType=" + creatureType + '}';
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 31 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 31 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Creature other = (Creature) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        return true;
    }
}
